
import json
import requests
from . import utils
from . import const


def get_access_token():
    ts_now = utils.get_now_stamp13()
    token_sign = utils.gen_token_sign(const.APP_KEY, const.APP_SECRET, const.NONCE, ts_now)
    # print("token_sign is ", token_sign)
    headers1 = utils.json_default_headers()
    token_url = const.TOKEN_URL
    data = {"appkey": const.APP_KEY, "sign": token_sign, "timestamp": ts_now, "nonce": const.NONCE}
    # print("data is ", data)

    # print("发送requests请求")
    rsp = requests.post(token_url, headers=headers1, data=json.dumps(data).encode('utf-8'))

    if rsp.status_code == 200:
        json_body = rsp.json()
        if json_body["status"] == 0:
            return json_body.get("message", {}).get("access_token", "")
    return ""


def exec_water_api(token: str, url: str, header_dict: dict, body_dict: dict, arr_attr: str = "message.rows"):
    headers1 = utils.json_default_headers()
    if header_dict is not None:
        headers1.update(header_dict)
    headers1["watermanage_access_token"] = token
    # print("入参:"+json.dumps(body_dict))
    rsp = requests.post(url, headers=headers1, data=json.dumps(body_dict))
    if rsp.status_code == 200:
        json_body = rsp.json()
        # print(json_body)
        if json_body["status"] == 0:
            if arr_attr == "message.rows":
                api_result = json_body.get("message", {}).get("rows", [])
                if isinstance(api_result, list):
                    return api_result
                else:
                    print("exec_water_api 结果不是一个list")
                    return []
            if arr_attr == "message":
                api_result = json_body.get("message", [])
                if isinstance(api_result, list):
                    return api_result
                else:
                    print("exec_water_api 结果不是一个list")
                    return []
    else:
        print("rsp status is ", rsp.status_code)
        print("content is ", rsp.content)
    return []


def get_generate_token(token_tag: str):
    ts_now = utils.get_now_stamp13()
    token_sign = utils.gen_token_sign(const.APP_KEY, const.APP_SECRET, const.NONCE, ts_now)
    # print("3 token_sign is ", token_sign)
    headers1 = utils.json_default_headers()
    token_url = f"https://sldn.slt.zj.gov.cn:30003/service/v2/auth/generateToken/{token_tag}"
    data = {"appkey": const.APP_KEY, "sign": token_sign, "timestamp": ts_now, "nonce": const.NONCE}
    # print("3 data is ", data)

    # print("3 发送requests请求")
    rsp = requests.post(token_url, headers=headers1, data=json.dumps(data).encode('utf-8'))
    if rsp.status_code == 200:
        json_body = rsp.json()
        # print("get_access_token3 rsp")
        # print(json_body)
        if json_body["status"] == 0:
            return json_body.get("message", {}).get("token", ""), json_body.get("message", {}).get("url", "")
    # else:
    #     print("token3 rsp status is ", rsp.status_code)
    #     print("token3 content is ", rsp.content)
    return "", ""


def get_station_token():
    return get_generate_token("55e51c47")


def get_station_waterdata_token():
    return get_generate_token("f896e295")


def exec_water2_api(token: str, url: str, header_dict: dict, body_dict: dict, arr_attr: str = "message"):
    if token == "":
        print("toke is empty!!!")
        return ""
    headers1 = utils.json_default_headers()
    if header_dict is not None:
        headers1.update(header_dict)
    headers1["token"] = token
    print("入参:" + json.dumps(body_dict))
    rsp = requests.post(url, headers=headers1, data=json.dumps(body_dict))
    if rsp.status_code == 200:
        json_body = rsp.json()
        # print(json_body)
        if json_body["status"] == 0:
            if arr_attr == "message":
                api_result = json_body.get("message", [])
                if isinstance(api_result, list):
                    return api_result
                else:
                    print("exec_water2_api 结果不是一个list")
                    return []
            if arr_attr == "message.rows":
                api_result = json_body.get("message", {}).get("rows", [])
                if isinstance(api_result, list):
                    return api_result
                else:
                    print("exec_water2_api 结果不是一个list")
                    return []
    else:
        print("rsp status is ", rsp.status_code)
        print("content is ", rsp.content.decode("utf-8"))
    return []
